package com.mlan.userap.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.mlan.userap.model.ChatGroupInfo;
import com.mlan.userap.okhttp.ApiResponse;
import com.mlan.userap.okhttp.JsonCallBack;
import com.mlan.userap.okhttp.OkHttpHelper;

import java.util.HashMap;
import java.util.Map;

public class CallViewModel extends ViewModel {



    // 0。未接通。1 开始打电话。其他是挂电话
    public MutableLiveData<Integer> callStatus = new MutableLiveData<>();
    //0 是主动 1 是被动
    public MutableLiveData<Integer> stype = new MutableLiveData<>();
    public MutableLiveData<Integer> callType = new MutableLiveData<>();
    public MutableLiveData<Integer> upCallType = new MutableLiveData<>();
    public MutableLiveData<ChatGroupInfo> groupEntity = new MutableLiveData<>();

    public String groupId = "";
    public String groupAvatar = "";
    public String userSig = "";
    public int sdkAppId = 0;
    public int roomId = 0;
    //自己的userid
    public Long userid = 0L;
    //对方的userid
    public Long otherUserid = 0L;
    public String name = "";

    public void setData() {
    }

    //解析被动打电话的数据
    public void callService(String json) {
        JsonMap jsonMap = new JsonMap(json);
        groupId = jsonMap.getString("groupId");
        JsonMap enterInfo = jsonMap.getJsonMap("enterInfo");
        sdkAppId = enterInfo.getInt("sdkAppId");
        userSig = enterInfo.getString("userSig");
        roomId = enterInfo.getInt("roomId");
        userid = enterInfo.getLong("userId");
        JsonMap createUser = jsonMap.getJsonMap("createUser");
        JsonList userList = jsonMap.getList("userList");
        for (int i = 0; i < userList.size(); i++) {
            long jsonValueItem = userList.getJsonMap(i).getLong("userId");
            if (jsonValueItem != createUser.getLong("userId")) {
                otherUserid = userList.getJsonMap(i).getLong("userId");
                groupAvatar = userList.getJsonMap(i).getString("headUrl");
            }
        }
        Log.d("zdl","=========callService==========="+roomId);
        callStatus.setValue(0);
    }


    public void upCallService(){
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("roomId", roomId + "");
        OkHttpHelper.get("/Chat/EnterRoom", stringStringMap, new JsonCallBack<JsonMap>() {
            @Override
            public void onSuccess(ApiResponse<JsonMap> result, String value2) {
                //请求数据
                callStatus.setValue(1);
                upCallType.setValue(1);
            }

            @Override
            public void onError(Exception error) {

            }
        });
    }


    //主动发起
    public void callService() {
        if (callType.getValue() == 0) {
            Map<String, String> stringStringMap = new HashMap<>();
            stringStringMap.put("groupId", groupId);
            stringStringMap.put("stype", stype.getValue().toString());//	1：语音 2:视屏
            OkHttpHelper.get("/Chat/CreateRoom", stringStringMap, new JsonCallBack<String>() {
                @Override
                public void onSuccess(ApiResponse<String> result, String value2) {
                    //                        {groupId=2, stype=0.0, startTicks=0.0, enterInfo={sdkAppId=1600077620, userId=124,
//                                userSig=eJwtzU0LgkAYBOD-stdC9l3cD4UOeQi1JCnD7ObqGosoi5oE0X-P1OM8wzAflJyu1qg65CJiYbSdsy5VO*hKz0zLAgTQqrClsIFiSWwBkuXgEGCEOeumL*vcGF0iFxjGmHNG8NKot9GdmpxSSqZq0UE3f*M2BwcwW7XXz*nSxK-b0Q9F4PFz4Ify0GajI*NHZPYbD*o0Yl2SXtgdmmyHvj*q3DZ*, roomId=1036.0},
//                                createUser={userId=124.0, nick=102, headUrl=http://admin.mlansoft.com/head/101.jpg, joinTicks=0.0},
//                             userList=[{userId=124.0, nick=102, headUrl=http://admin.mlansoft.com/head/101.jpg, joinTicks=0.0},
//                            {userId=126.0, nick=104, headUrl=http://admin.mlansoft.com/head/103.jpg, joinTicks=0.0}]}
                    JsonMap jsonResult = JsonMap.parse(value2);
                    Log.d("zdl", "=========jsonData=======" + jsonResult);
                    JsonMap jsonMap = jsonResult.getJsonMap("data");
                    groupId = jsonMap.getString("groupId");
                    JsonMap enterInfo = jsonMap.getJsonMap("enterInfo");
                    sdkAppId = enterInfo.getInt("sdkAppId");
                    userSig = enterInfo.getString("userSig");
                    roomId = enterInfo.getInt("roomId");
                    upCallType.setValue(0);
                }


                @Override
                public void onError(Exception error) {

                }
            });
        }
    }


    //挂断电话
    public void hangupService(CallBack callBack) {
        Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("roomId", roomId+"");
        stringStringMap.put("stype", "1");//	1：挂断 2:异常

        OkHttpHelper.get("/Chat/ExitRoom", stringStringMap, new JsonCallBack<String>() {
            @Override
            public void onSuccess(ApiResponse<String> result, String value2) {
                callBack.callBack();
            }


            @Override
            public void onError(Exception error) {

            }
        });
    }

    public interface CallBack {
        void callBack();
    }

}
